<?php
namespace app\admin\model;

use think\Model;

class User extends Model
{
    /**
     * 获取用户信息列表
     * @param array $where
     * @param $order
     * @param int $pageSize
     * @param int $page
     * @return mixed
     */
    public function getList($where,$sort_by,$sort_order,$pageSize = 20,$page = 1){
        $result = $this->alias('a')
            ->join(['zhimeng_user_group'=>'b'],'a.user_group_id=b.user_group_id','left')
            ->join(['zhimeng_user_agent'=>'c'],'a.user_agent_id=c.user_agent_id','left')
            ->field('a.user_id,a.photo,a.status,a.photo,a.username,a.mobile,a.address,a.money,a.realname,a.idcard,a.lastlogin_time,b.group_name,c.agent_username,a.create_time')
            ->where($where)
            ->order($sort_by . ' ' . $sort_order)
            ->paginate(array('list_rows' => $pageSize, 'page' => $page))
            ->toArray();
        return $result;
    }

    /**
     * 新增用户
     * @param $data
     * @return mixed
     */
    public function add($data){
        $data['create_time'] = time();
        return $this->save($data);
    }

    /**
     * 根据用户id修改user表信息
     * @param $user_id
     * @param $data
     * @return bool
     */
    public function updateByUserId($user_id,$data){
        $user_id = intval($user_id);
        if (empty($user_id) || empty($data) || !is_array($data)){
            return false;
        }
        if (isset($data['password'])){
            $data['password'] = md5($data['password']);
        }
        if (isset($data['pay_password'])){
            $data['pay_password'] = md5($data['pay_password']);
        }
        $where = [ 'user_id' => $user_id ];
        return $this->where($where)->save($data);
    }

    /**
     * @param $user_id
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserInfo($user_id){
        return $this->where(['user_id'=>$user_id])->withoutField('password,create_time,lastlogin_time')->find();
    }


    /**
     * 获取用户资金明细
     */
    public function getUserMoneyLog($user_id){

    }
}